<?php
require( 'php/database.php' );
require( 'config.php' );

//
session_start( );

if ( isset( $_GET[ 'language_id' ] ) )
    $_SESSION[ 'language_id' ] = $_GET[ 'language_id' ];
else {
    if ( isset( $_SESSION[ 'language_id' ] ) == false )
        $_SESSION[ 'language_id' ] = 1;
}

if ( isset( $_GET[ 'category_id' ] ) )
    $_SESSION[ 'category_id' ] = $_GET[ 'category_id' ];
else {
    if ( isset( $_SESSION[ 'category_id' ] ) == false )
        $_SESSION[ 'category_id' ] = 0;
}

// Performing SQL query
$sSQL  = "SELECT p.*, m.message_text AS product_text";
$sSQL .= "  FROM products p, messages m";
$sSQL .= " WHERE p.product_id = m.product_id";
$sSQL .= "   AND m.message_type = 0";
$sSQL .= "   AND m.language_id = ${_SESSION[ 'language_id' ]}";
if ( $_SESSION[ 'category_id' ] != 0 )
    $sSQL .= "   AND p.category_id = ${_SESSION[ 'category_id' ]}";
$sSQL .= " ORDER BY p.product_code";

//
$oDB = new Database( $g_sMySqlServer,
                     $g_sMySqlUsername,
                     $g_sMySqlPassword,
                     $g_sMySqlDatabase );

$aRows = $oDB->fetch( $sSQL );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>
      Warehouse - Browse
    </title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/utils.js">
    </script>
    <script type="text/javascript">
    function update_language( _language_id )
    {
        window.location.href = 'browse.php?language_id=' + _language_id;
    }

    function update_category( _category_id )
    {
        window.location.href = 'browse.php?category_id=' + _category_id;
    }

    function select_all( _form, _count, _prefix )
    {
        var i;
        
        for (i = 0; i < _count; i++ )
            _form.elements[ _prefix + i ].checked = true;
    }

    function select_none( _form, _count, _prefix )
    {
        var i;
        
        for (i = 0; i < _count; i++ )
            _form.elements[ _prefix + i ].checked = false;
    }
    </script>
  </head>
  <body>
    <form action="report.php" method="get" name="items" id="items">
      <input type="hidden" name="item_count" id="item_count" value="<?php echo count( $aRows ); ?>">
      <table width="100%" cellspacing="0">
        <tr>
          <th width="1">
            &nbsp;
          </th>
          <th width="10%">
            Code
          </th>
          <th width="*">
            Name
          </th>
          <th width="5%">
            Prc
          </th>
          <th width="5%">
            Qty
          </th>
          <th width="1">
            &nbsp;
          </th>
          <th width="1">
            &nbsp;
          </th>
        </tr>
<?php
    //
    $i = 0;
    
    foreach ( $aRows as $aRow ) {
?>
        <tr class="<?php echo (($i % 2) == 0) ? 'evenRow' : 'oddRow'; ?>">
          <td align="center" width="1">
            <input type="checkbox" name="item_<?php echo $i; ?>" value="<?php echo $aRow[ 'product_id' ]; ?>">
          </td>
          <td align="center" width="10%">
            <?php echo $aRow[ 'product_code' ]; ?>
            </td>
          <td align="left" width="*">
            <?php echo $aRow[ 'product_text' ]; ?>
            </td>
          <td align="right" width="5%">
            <?php echo $aRow[ 'product_price' ]; ?>
            </td>
          <td align="right" width="5%">
            <input type="text" name="item_<?php echo $i; ?>_quantity" size="4" maxlength="4" value="0">
          </td>
          <td align="center" width="1">
            <a href="details.php?id=<?php echo $aRow[ 'product_id' ]; ?>"><img src="icons/zoom.png" alt="zoom"></a>
          </td>
          <td align="center" width="1">
            <a href="delete_cgi.php?id=<?php echo $aRow[ 'product_id' ]; ?>"><img src="icons/cross.png" onclick="return confirm_delete( '<?php echo $aRow[ 'product_code' ]; ?>' );" alt="delete"></a>
          </td>
        </tr>
<?php
        $i += 1;
    }
?>
        <tr>
          <td colspan="7" valign="middle">
            <img src="icons/arrow_ltr.png" alt="arrow">
            Select
            <a href="#" onclick="select_all( document.forms[ 'items' ], <?php echo $i ?>, 'item_' );">all</a>
            /
            <a href="#" onclick="select_none( document.forms[ 'items' ], <?php echo $i ?>, 'item_' );">none</a>
<!--
            <input type="submit" value="Delete">
-->
            <input type="submit" value="Report">
<?php
    $aLanguages = $oDB->fetch( "SELECT language_id, language_text FROM languages" );
    echo '<select id="language_id" name="language_id" onchange="update_language( form.elements[ \'language_id\' ].value );">';
    foreach ( $aLanguages as $aLanguage ) {
      echo '<option value="' . $aLanguage[ 'language_id' ] . '"' . ( ( $_SESSION[ 'language_id' ] == $aLanguage[ 'language_id' ] )  ? ' selected' : '') . '>';
      echo $aLanguage[ 'language_text' ];
      echo '</option>';
    }
    echo '</select>';
?>
<?php
    $aCategories = $oDB->fetch( "SELECT category_id, category_text FROM categories" );
    $aCategories[] = array( 'category_id' => 0,
                            'category_text' => '&lt;all&gt;' );
    sort( $aCategories );
    echo '<select id="category_id" name="category_id" onchange="update_category( form.elements[ \'category_id\' ].value );">';
    foreach ( $aCategories as $aCategory ) {
      echo '<option value="' . $aCategory[ 'category_id' ] . '"' . ( ( $_SESSION[ 'category_id' ] == $aCategory[ 'category_id' ] )  ? ' selected' : '') . '>';
      echo $aCategory[ 'category_text' ];
      echo '</option>';
    }
    echo '</select>';
?>
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>
